package com.moli.iov.down.command.codec.decode.decoders;

import com.moli.iov.down.command.codec.decode.bean.ControlResBean;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  终端控制应答 解码
 * </p>
 *
 * @author: zql
 * @Date: 2018/11/30 17:10
 */
@Component("controlDecoder")
@Scope("prototype")
public class ControlDecoder extends BaseDecoder{

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    public ControlResBean decode(byte[] data){

        try {
            log.info("终端控制应答 解码 入参：{}", data);
            byte[] date = ArrayUtils.subarray(data, 0, 6);
            data = ArrayUtils.subarray(data, 6, data.length);

            byte[] serialNumber = ArrayUtils.subarray(data, 0, 2);
            data = ArrayUtils.subarray(data, 2, data.length);

            byte[] result = ArrayUtils.subarray(data,0 ,1);

            ControlResBean controlResBean = new ControlResBean();
            controlResBean.setDate(this.dateDecode(date));
            controlResBean.setSerialNumber(this.serialNumberDecode(serialNumber));
            controlResBean.setResult(result[0]);
            log.info("终端控制应答 解码 结果: {}", controlResBean);
            return controlResBean;
        } catch (Exception e) {
            log.error("终端控制应答 解码 异常", e);
            return null;
        }

    }
}
